package com.dongdongshop.controller;


import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.service.TbSpecificationService;
import com.dongdongshop.vo.SpecVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("specification")
public class TbSpecificationController {

    //打印日志
    private Logger logger = LoggerFactory.getLogger(TbBrandController.class);

    @DubboReference
    private TbSpecificationService specificationService;

    //跳转方法
    @RequestMapping("show")
    public String show(){
        return "/admin/specification";
    }

    //分页查询spec信息 根据名字模糊查询
    @RequestMapping("listSpecifications")
    @ResponseBody
    public Result listSpecifications(@RequestParam(defaultValue = "1") Integer pageNum,
                                     @RequestParam(defaultValue = "3") Integer pageSize,
                                     String name){
        try {
            PageResult page = specificationService.listSpecifications(pageNum,pageSize,name);
            return Result.OK().setData(page);
        }catch (Exception e){
            logger.info("查询规格出现异常: {}",e);
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }

    //添加spec数据单个,添加所属的option,接受字符串转成list<specoption>集合对象用JSONUtil
    @RequestMapping("addSpec")
    @ResponseBody
    public Result addSpec(TbSpecification specification, String SpecOption){
        try {
            specificationService.addSpecAndOptions(specification,SpecOption);
            return Result.OK();
        }catch (Exception e){
            logger.info("添加规格出现异常: {}",e);
            return Result.response(ResultEnum.ADD_MESSAGE_ERROE);
        }

    }
    //回显方法根据id查出对象放入VO类中
    @RequestMapping("toUpdate")
    @ResponseBody
    public Result toUpdate(Long id){
        try {
            SpecVO vo = specificationService.selectSpecAndOptionById(id);
            return Result.OK().setData(vo);
        }catch (Exception e){
            logger.info("添加规格出现异常: {}",e);
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }
    //修改方法
    @RequestMapping("updateSpec")
    @ResponseBody
    public Result updateSpec(TbSpecification spec,String SpecOption){
        try {
            specificationService.updateSpecAndSperOption(spec,SpecOption);
            return Result.OK();
        }catch (Exception e){
            logger.info("修改规格出现异常: {}",e);
            return Result.response(ResultEnum.UPDATE_MESSAGE_ERROE);
        }
    }
    //批量删除
    @RequestMapping("deleteSpecs")
    @ResponseBody
    public Result deleteSpecs(Long[] ids){
        try {
            specificationService.deleteSpecs(ids);
            return Result.OK();
        }catch (Exception e){
            logger.info("删除规格出现异常: {}",e);
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }
}
